package com.zhanglinwei.openApiClient.utils;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

public class CollUtils {

    private CollUtils(){}

    public static <R, T extends Collection<R>> Collection<R> removeLastIf(T coll, Predicate<R> condition) {
        if (CollectionUtils.isEmpty(coll)) {
            return new ArrayList<>();
        }

        List<R> list = new ArrayList<>(coll);
        int lastIndex = list.size() - 1;
        R lastElement = list.get(lastIndex);

        if (condition == null || condition.test(lastElement)) {
            list.remove(lastIndex);
        }

        return list;
    }

}
